#include "LevelLoader.h"
#include "LevelTrigger.h"


LevelLoader::LevelLoader(void)
{
}


LevelLoader::~LevelLoader(void)
{
}

Level LevelLoader::loadLevel(string file)
{
	float PI = 3.14159265f;
	//abusing SettingsManager to avoid loading everything manually
	SettingsManager xml(file);

	xml_node<>* current = xml.getFirstNode();

	//scene -> nodes -> first node
	current = current->first_node()->first_node();

	btVector3 pos, scale;
	btQuaternion rot;
	string name, mesh;
	Level lvl;
	while(current!=NULL)
	{
		//skip lights and cameras.
		if(current->first_node("camera") != NULL || current->first_node("light") != NULL || current->first_node("Hemi") != NULL)
		{
			current = current->next_sibling();
			continue;
		}
		name = current->first_attribute("name")->value();
		if(name.find("Hemi") == string::npos)
		{
			pos = btVector3(btScalar(atof(current->first_node("position")->first_attribute("x")->value())),
							btScalar(atof(current->first_node("position")->first_attribute("y")->value())),
							btScalar(atof(current->first_node("position")->first_attribute("z")->value())));

			rot = btQuaternion(btScalar(atof(current->first_node("rotation")->first_attribute("qx")->value())),
								btScalar(atof(current->first_node("rotation")->first_attribute("qy")->value())),
								btScalar(atof(current->first_node("rotation")->first_attribute("qz")->value())),
								btScalar(atof(current->first_node("rotation")->first_attribute("qw")->value())));

			scale = btVector3(btScalar(atof(current->first_node("scale")->first_attribute("x")->value())),
							btScalar(atof(current->first_node("scale")->first_attribute("y")->value())),
							btScalar(atof(current->first_node("scale")->first_attribute("z")->value())));


			mesh = current->first_node("entity")->first_attribute("meshFile")->value();

			handleObject(lvl, name, pos, rot, scale, mesh);
		}


		current = current->next_sibling();
						
	}
	return lvl;
}

void LevelLoader::handleObject(Level &lvl, string name, btVector3 &pos, btQuaternion &rot, btVector3 &scale, string mesh)
{
	if(name.find("straight") != string::npos || name.find("outer_corner") != string::npos)
	{
		BaseObject* obj = new BaseObject("level/gravity_col.xml", name, pos, rot, scale, mesh);
		obj->addType(OBJECT_LEVEL);
		if(name.find("outer_corner") != string::npos)
			obj->addType(OBJECT_CORNER);
		lvl.getObjectList().push_back(VisibleObjectPtr(obj));
	}if(name.find("grass_corner") != string::npos)
	{
		BaseObject* obj = new BaseObject("level/gravity_col_corner.xml", name, pos, rot, scale, mesh);
		obj->addType(OBJECT_LEVEL);
		obj->addType(OBJECT_HORIZONTAL_CORNER);
		lvl.getObjectList().push_back(VisibleObjectPtr(obj));
	}if(name.find("earth") != string::npos || name.find("inner_corner") != string::npos || name.find("grass_bevel") != string::npos)
	{
		BaseObject* obj = new BaseObject("level/col.xml", name, pos, rot, scale, mesh);
		obj->addType(OBJECT_LEVEL);
		lvl.getObjectList().push_back(VisibleObjectPtr(obj));
	}if(name.find("visual") != string::npos)
	{
		BaseObject* obj = new BaseObject("level/visual.xml", name, pos, rot, scale, mesh);
		obj->addType(OBJECT_VISUAL);
		lvl.getObjectList().push_back(VisibleObjectPtr(obj));
	}if(name.find("trigger") != string::npos)
	{
		BaseObject* obj = (BaseObject*)new LevelTrigger("level/leveltrigger.xml", name, pos, rot, scale, mesh);
		lvl.getObjectList().push_back(VisibleObjectPtr(obj));
	}if(name.find("start") != string::npos)
	{
		lvl.getStart().setRotation(rot);
		lvl.getStart().setOrigin(pos);
	}if(name.find("blobb") != string::npos)
	{
		btTransform trans;
		trans.setRotation(rot);
		trans.setOrigin(pos);
		lvl.getSpawns().push_back(Spawn(trans, OBJECT_BLOBB));
	}if(name.find("mirrorboss") != string::npos)
	{
		btTransform trans;
		trans.setRotation(rot);
		trans.setOrigin(pos);
		lvl.getSpawns().push_back(Spawn(trans, OBJECT_MIRROR_BOSS));
	}if(name.find("split_blobb") != string::npos)
	{
		btTransform trans;
		trans.setRotation(rot);
		trans.setOrigin(pos);
		lvl.getSpawns().push_back(Spawn(trans, OBJECT_SPLIT_BLOBB));
	}if(name.find("ranged_fighter") != string::npos)
	{
		btTransform trans;
		trans.setRotation(rot);
		trans.setOrigin(pos);
		lvl.getSpawns().push_back(Spawn(trans, OBJECT_RANGED_FIGHTER));
	}if(name.find("block_fighter") != string::npos)
	{
		btTransform trans;
		trans.setRotation(rot);
		trans.setOrigin(pos);
		lvl.getSpawns().push_back(Spawn(trans, OBJECT_BLOCK_FIGHTER));
	}if(name.find("little_fighter") != string::npos)
	{
		btTransform trans;
		trans.setRotation(rot);
		trans.setOrigin(pos);
		lvl.getSpawns().push_back(Spawn(trans, OBJECT_LITTLE_FIGHTER));
	}if(name.find("gravity_blobb") != string::npos)
	{
		btTransform trans;
		trans.setRotation(rot);
		trans.setOrigin(pos);
		lvl.getSpawns().push_back(Spawn(trans, OBJECT_GRAVITYBLOBB));
	}if(name.find("hedgehog_blobb") != string::npos)
	{
		btTransform trans;
		trans.setRotation(rot);
		trans.setOrigin(pos);
		lvl.getSpawns().push_back(Spawn(trans, OBJECT_HEDGEHOGBLOBB));
	}
}